package com.zrrd.learn.chapter07字符串;

/**
 * 字符串习题04
 *
 * @author wrn
 * @version 1.0
 * @date 2023/7/18 15:13
 */

public class StringExercise04 {

    /**
     * 1.输入一行字符，分别统计出其中英文字母、空格、数字和其它字符的个数。
     *
     * @param str
     */
    public static void question01(String str) {
        System.out.println("第一题");
        int letter = 0;
        int space = 0;
        int digit = 0;
        int other = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z' || str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {
                letter++;
            } else if (str.charAt(i) == ' ') {
                space++;
            } else if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
                digit++;
            } else {
                other++;
            }
        }
        System.out.println("字符串中\n英文字母的个数为" + letter + "\n空格的个数为" + space + "\n数字的个数为" + digit + "\n其它字符的个数为" + other);
    }

    /**
     * 2.求长度：int myStrLen(const char *str)
     * @param str
     */
    public static void question02(String str) {


    }

    /**
     * 3.把 lengendary 改成间隔大写小写模式，即 LeNgEnDaRy
     *
     * @param str
     */
    public static void question03(String str) {

        System.out.println("第三题");
        System.out.println("字符串间隔大写小写模式为");
        char[] cstr = str.toCharArray();
        for (int i = 0; i < str.length(); i++) {
            if (i % 2 == 0) {
                cstr[i] = Character.toUpperCase(cstr[i]);
            }
        }
        System.out.println(cstr);

    }

    /**
     * 4.把 lengendary 最后一个字母变大写
     *
     * @param str
     */
    public static void question04(String str) {
        System.out.println("第四题");
        System.out.println("字符串最后一个字母变大写后为");
        char[] cstr = str.toCharArray();
        for (int i = str.length()-1; i >= 0; i--) {
            cstr[i] = Character.toUpperCase(cstr[i]);
            break;
        }

        System.out.println(cstr);
    }

    /**
     * 5.把最后一个two单词首字母大写
     *
     * Nature has given us that two ears, two eyes, and but one tongue, to the end that we should hear and see more than we speak
     * @param str
     */
    public static void question05(String str) {

        System.out.println("第五题");
        System.out.println("字符串把最后一个two单词首字母大写后为");
        char[] cstr = str.toCharArray();
        for (int i = str.length()-1; i >= 0; i--) {
            if (str.charAt(i)=='o'){
                if (str.charAt(i-1)=='w'){
                    if (str.charAt(i-2)=='t'){
                        cstr[i-2] = Character.toUpperCase(cstr[i-2]);
                        break;
                    }
                }
            }

        }

        System.out.println(cstr);

    }

    public static void main(String[] args) {

        question01("!  #12aD");

        question03("lengendary");

        question04("lengendary");

        question05("Nature has given us that two ears, two eyes, and but one tongue, to the end that we should hear and see more than we speak");

    }

}
